import { C_EndIndex, C_IsTry, C_StartIndex } from "./C_Config";
import C_GameScrew from "./C_GameScrew";
import C_App from "./core/C_App";
import { C_NotificationConst } from "./core/C_NotificationConst";
import C_NodePool from "./core/utils/C_NodePool";
import C_SoundManager from "./core/utils/C_SoundManager";
import C_CustomsView from "./game/view/C_CustomsView";
import C_FailedView from "./game/view/C_FailedView";
import C_FirstView from "./game/view/C_FirstView";
import C_WinView from "./game/view/C_WinView";

const { ccclass, property } = cc._decorator;

/** 游戏主玩法 */
@ccclass
export default class C_Game extends cc.Component {
    @property(cc.Prefab)
    gameScrew: cc.Prefab = null;

    @property(cc.Prefab)
    customsView: cc.Prefab = null;

    @property(cc.Node)
    gameContent: cc.Node = null;

    @property(C_NodePool)
    winPool: C_NodePool = null;

    @property(C_NodePool)
    failedPool: C_NodePool = null;

    @property(C_NodePool)
    firstWinPool: C_NodePool = null;

    _firstWinView: C_FirstView = null;
    _failedView: C_FailedView = null;
    _winView: C_WinView = null;
    protected _customsView: C_CustomsView = null;
    protected _gameScrew: C_GameScrew = null;
    curIndex: number = 0;

    static instance: C_Game;
    protected onDestroy(): void {
        C_Game.instance = null;
        C_SoundManager.instance = null;
    }

    protected onLoad(): void {
        C_App.init();
        cc.director.getPhysicsManager().enabled = true;
        const manager = cc.director.getCollisionManager();
        manager.enabled = true;
        // manager.enabledDebugDraw = CC_DEBUG;
        C_Game.instance = this;
        C_App.NotificationCenter.addListener(C_NotificationConst.ENTER_CUSTOMS, this.enterCustoms, this);
        C_App.NotificationCenter.addListener(C_NotificationConst.BACK_CUSTOMS, this.backCustoms, this);
        C_App.NotificationCenter.addListener(C_NotificationConst.PASS_CUSTOMS, this.onPassCustoms, this);
        if (C_IsTry) {
            this.curIndex = C_StartIndex - 1
            this.enterCustoms(this.curIndex);
            cc.tween(this.node)
                .delay(32)
                .call(() => {
                    this.endTryGame()
                })
                .start()
        }
        else {
            const node = cc.instantiate(this.customsView);
            const customsView = this._customsView = node.getComponent(C_CustomsView);
            const saveData = cc.sys.localStorage.getItem('C_Screw');
            this.curIndex = parseInt(saveData) || 0;
            customsView.initView(this.curIndex);
            this.node.addChild(node);
        }


    }

    async endTryGame() {
        await new Promise((resolve: Function, reject: Function) => {
            console.log("试玩结束")
            if (cc.sys.platform === cc.sys.WECHAT_GAME) {
                //@ts-ignore
                wx.notifyMiniProgramPlayableStatus({
                    isEnd: true
                });
                resolve(true);
            }
        });

    }

    /** 进入指定关卡 */
    enterCustoms(index: number) {
        if (this._customsView)
            this._customsView.node.removeFromParent();
        if (!this._gameScrew) {
            const node = cc.instantiate(this.gameScrew);
            this._gameScrew = node.getComponent(C_GameScrew);

        }
        this._gameScrew.node.parent = this.gameContent;
        // this.gameContent.addChild(this._gameScrew.node);
        this._gameScrew.initGame(index);
    }

    /** 返回选关界面 */
    backCustoms(isFailed?: number) {
        if (this._firstWinView) {
            this.firstWinPool.push(this._firstWinView.node);
            this._firstWinView = null;
        }
        if (this._failedView) {
            this.failedPool.push(this._failedView.node);
            this._failedView = null;
        }
        if (this._winView) {
            this.winPool.push(this._winView.node);
            this._winView = null;
        }
        this._gameScrew.exitCustoms();
        if (C_IsTry) {
            if (isFailed) {
                this.enterCustoms(this.curIndex);
            }
            else {
                this.curIndex += 1;
                this.enterCustoms(this.curIndex);
            }
        }
        else {
            this._gameScrew.node.removeFromParent();
            this.node.addChild(this._customsView.node);
            this._customsView.initView(this.curIndex);
        }
    }

    /** 通关 */
    onPassCustoms(index: number) {
        if (C_IsTry) {
            // const node = this.winPool.pop();
            // const view = this._winView = node.getComponent(C_WinView);
            // view.initView(index);
            // this.node.addChild(node);
            this.winNode.scale = .5;
            this.winNode.angle = 90
            this.winNode.active = true;
            cc.tween(this.winNode)
                .to(.2, { scale: 1, angle: 0 })
                .delay(.3)
                .to(.2, { scale: .5 })
                .call(() => {
                    this.onTouchWin();
                })
                .start()
            if (this.curIndex >= C_EndIndex - 1) {
                cc.tween(this.node)
                    .delay(1)
                    .call(() => {
                        this.endTryGame()
                    })
                    .start()
            }

        }
        else {
            if (this.curIndex <= index) {
                const node = this.firstWinPool.pop();
                this.node.addChild(node);
                const view = this._firstWinView = node.getComponent(C_FirstView);
                view.initView(index);
                this.curIndex = ++index;
                cc.sys.localStorage.setItem("C_Screw", this.curIndex);
            }
            else {
                const node = this.winPool.pop();
                const view = this._winView = node.getComponent(C_WinView);
                view.initView(index);
                this.node.addChild(node);
            }
        }
    }

    @property(cc.Node)
    winNode: cc.Node = null;

    /** 闯关失败 */
    onFailedCustoms() {
        const node = this.failedPool.pop();
        const view = this._failedView = node.getComponent(C_FailedView);
        view.initView();
        this.node.addChild(node);
    }

    onTouchWin() {
        this.backCustoms();
        this.winNode.active = false;
    }
}

